-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests_tree.py
65 lines (56 loc) · 1.31 KB
/
tests_tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import sys
import unittest
from io import StringIO
from tree_implementation import Tree
ORIGINAL_STDOUT = sys.stdout
class TreeTests(unittest.TestCase):
def test_build_tree_for_loop_traversal(self):
tree = Tree(7, [
Tree(19,[
Tree(1),
Tree(12),
Tree(31)
]),
Tree(21),
Tree(14,[
Tree(23),
Tree(6)
])
])
nodes = []
expected_nodes = [7, 19, 1, 12, 31, 21, 14, 23, 6]
for node in tree:
nodes.append(node)
self.assertEqual(nodes, expected_nodes)
def test_build_tree_print_tree(self):
out = StringIO()
sys.stdout = out
tree = Tree(7, [
Tree(19, [
Tree(1),
Tree(12),
Tree(31)
]),
Tree(21),
Tree(14, [
Tree(23),
Tree(6)
])
])
try:
tree.print()
output = out.getvalue().strip()
expected_output = """7
19
1
12
31
21
14
23
6"""
self.assertEqual(output, expected_output)
finally: # restore STDOUT
sys.stdout = ORIGINAL_STDOUT
if __name__ == '__main__':
unittest.main()